﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace OneMake.Model
{
    //全网各类目热销特征
    public class report_qw_sale_feature
    {
        public List<string> titles { get; set; }
        public Dictionary<string, Dictionary<DateTime, double>> values { get; set; }
        public report_qw_sale_feature()
        {
            titles = new List<string>();
            values = new Dictionary<string, Dictionary<DateTime, double>>();
        }
    }

    public class report_qw_sale_feature2
    {
        public string feature { get; set; }
        public double n_deal { get; set; }
        public DateTime stat_datetime { get; set; }
    }

    partial class ReportHelper
    {
        public List<string> get_report_qw_sale_feature_titles(DateTime beginTime, DateTime endTime)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            DateTime tempTime = beginTime;
            tempTime.AddDays(1 - tempTime.Day);

            List<string> titles = new List<string>();
            titles.Add("行标签");
            while (tempTime <= endTime)
            {
                titles.Add(tempTime.ToShortDateString());
                tempTime = tempTime.AddMonths(1);
            }
            return titles;
        }

        public report_qw_sale_feature get_report_report_qw_sale_feature(string hotsale_feature, string type, string category, DateTime beginTime, DateTime endTime)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = @"select feature,sum(n_deal) as n_deal,stat_datetime from mf_m_tz 
                        where hotsale_feature=@0 and type=@1 and category=@2
                        and stat_datetime between @3 and @4
                        group by stat_datetime";

            IEnumerable<report_qw_sale_feature2> items = db.Query<report_qw_sale_feature2>(sql, hotsale_feature,type, category, beginTime, endTime);
            report_qw_sale_feature report = new report_qw_sale_feature();
            report.titles.Add("行标签");

            foreach (report_qw_sale_feature2 item in items)
            {
                report.titles.Add(item.stat_datetime.ToString("yyyy年MM月"));

                if (!report.values.ContainsKey(item.feature))
                    report.values.Add(item.feature, new Dictionary<DateTime, double>());

                Dictionary<DateTime, double> selValues = report.values[item.feature];
                if (selValues.ContainsKey(item.stat_datetime))
                    selValues[item.stat_datetime] += item.n_deal;
                else
                    selValues.Add(item.stat_datetime, item.n_deal);
            }

            return report;
        }
        //------------------------------------------------------------
        public List<string> get_report_qw_sale_feature_hotsale_feature()
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = "select hotsale_feature from mf_m_tz group by hotsale_feature";
            IDataReader reader = db.ExecuteReader(sql);

            List<string> titles = new List<string>();
            while (reader.Read())
            {
                string title = reader["hotsale_feature"].ToString();
                titles.Add(title);
            }
            return titles;
        }

        public List<string> get_report_qw_sale_feature_type()
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = "select type from mf_m_tz group by type";
            IDataReader reader = db.ExecuteReader(sql);

            List<string> titles = new List<string>();
            while (reader.Read())
            {
                string title = reader["type"].ToString();
                titles.Add(title);
            }
            return titles;
        }

        public List<string> get_report_qw_sale_feature_category()
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = "select category from mf_m_tz group by category";
            IDataReader reader = db.ExecuteReader(sql);

            List<string> titles = new List<string>();
            while (reader.Read())
            {
                string title = reader["category"].ToString();
                titles.Add(title);
            }
            return titles;
        }
        
    }
}
